﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Service.Core.Redis.DistributedLock;
using Service.Core.Redis.Services;
using Service.Core.Redis.Services.Impl;
using Service.Framework.Utils;
using StackExchange.Redis;

namespace Service.Core.Redis
{
    public static class ServiceRedisExtensions
    {
        public static void AddRedisService(this WebApplicationBuilder builder)
        {
            var connectionString = builder.Configuration["ConnectionStrings:Redis"];
            builder.Services.AddSingleton<IConnectionMultiplexer>(sp =>
                ConnectionMultiplexer.Connect(connectionString));
            builder.Services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = connectionString;
                options.InstanceName = $"{Tools.GetAppName(builder.Environment.ApplicationName)}_"; // 可选，用于键前缀
            });
            builder.Services.AddSingleton(typeof(IRedisService), typeof(RedisServiceImpl));
            builder.Services.AddScoped(typeof(IDistributedLockService), typeof(DistributedLockService));
        }
    }
}
